package org.example.wu_and_dian.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.wu_and_dian.dto.response.AjaxResult;
import org.example.wu_and_dian.entity.EntityClass;
import org.example.wu_and_dian.mapper.EntityClassMapper;
import org.example.wu_and_dian.service.EntityClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @author SS2613
* @description 针对表【entity_class】的数据库操作Service实现
* @createDate 2025-03-31 14:11:34
*/
@Service
public class EntityClassServiceImpl extends ServiceImpl<EntityClassMapper, EntityClass>
    implements EntityClassService {
    @Autowired
    EntityClassMapper entityClassMapper;

    @Override
    public List<EntityClass> getByDocumentId(String documentId) {
        QueryWrapper<EntityClass> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("docu_id", documentId);
        return entityClassMapper.selectList(queryWrapper);
    }

    @Override
    public AjaxResult batchImportEntityClasses(List<EntityClass> requests) {
        int updatedCount = entityClassMapper.batchInsert(requests);
        return updatedCount > 0
                ? AjaxResult.success("更新文档内容成功")
                : AjaxResult.error("更新失败");
    }

    @Override
    @Transactional
    public AjaxResult insertEntityClass(EntityClass entityClass) {
        try {
            int result = entityClassMapper.insertEntityClass(entityClass);
            return result > 0 
                ? AjaxResult.success("添加实体类成功") 
                : AjaxResult.error("添加失败");
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    @Override
    public AjaxResult updateEntityClass(EntityClass entityClass) {
        try {
            int result = entityClassMapper.updateEntityClass(entityClass);
            return result > 0 
                ? AjaxResult.success("更新实体类成功")
                : AjaxResult.error("更新失败，记录不存在");
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    @Override
    public AjaxResult deleteEntityClass(EntityClass entityClass) {
        try {
            int result = entityClassMapper.deleteById(entityClass);
            return result > 0 
                ? AjaxResult.success("删除实体类成功")
                : AjaxResult.error("删除失败，未找到对应记录");
        } catch (NumberFormatException e) {
            return AjaxResult.error("ID格式错误");
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }
}




